package top.huhuiyu.springboot2.study.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.huhuiyu.springboot2.study.base.BaseDataResult;
import top.huhuiyu.springboot2.study.base.BaseResult;
import top.huhuiyu.springboot2.study.base.PageBean;
import top.huhuiyu.springboot2.study.base.PageResult;
import top.huhuiyu.springboot2.study.entity.Software;
import top.huhuiyu.springboot2.study.entity.SoftwareType;
import top.huhuiyu.springboot2.study.mapper.SoftwareMapper;
import top.huhuiyu.springboot2.study.mapper.SoftwareTypeMapper;
import top.huhuiyu.springboot2.study.service.SoftwareService;

import java.util.List;
import java.util.Optional;

/**
 * @author 胡辉煜
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SoftwareServiceImpl implements SoftwareService {
  private final SoftwareMapper softwareMapper;
  private final SoftwareTypeMapper softwareTypeMapper;

  @Override
  public BaseResult insert(Software software) {
    // 验证stid是否正确（存在记录）
    SoftwareType type = softwareTypeMapper.queryByKey(software.getStid());
    if (type == null) {
      return BaseResult.fail("软件类型不存在");
    }
    int result = softwareMapper.insert(software);
    return result == 1 ? BaseResult.ok("添加成功") : BaseResult.fail("添加失败");
  }

  @Override
  public BaseDataResult<Software> queryByKey(Integer sid) {
    Software software = softwareMapper.queryByKey(sid);
    if (software == null) {
      return BaseDataResult.fail("软件不存在", Software.class);
    }
    // 查询软件的分类信息
    SoftwareType type = softwareTypeMapper.queryByKey(software.getStid());
    software.setType(type);
    return BaseDataResult.ok(software);
  }

  @Override
  public BaseDataResult<PageResult<List<Software>>> query(Software software, PageBean pageBean) {
    PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
    Page<Software> page = (Page<Software>) softwareMapper.query(software);
    pageBean.setTotal(page.getTotal());
    // 处理软件分类信息
    page.getResult().forEach(s -> {
      // 查询软件的分类信息
      SoftwareType type = softwareTypeMapper.queryByKey(s.getStid());
      s.setType(type);
    });
    return BaseDataResult.ok(PageResult.instance(pageBean, page.getResult()));
  }

  @Override
  public BaseDataResult<PageResult<List<Software>>> queryMapping(Software software, PageBean pageBean) {
    PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
    Page<Software> page = (Page<Software>) softwareMapper.queryMapping(software);
    pageBean.setTotal(page.getTotal());
    return BaseDataResult.ok(PageResult.instance(pageBean, page.getResult()));
  }

  @Override
  public BaseDataResult<PageResult<List<Software>>> queryResult(Software software, PageBean pageBean) {
    PageHelper.startPage(pageBean.getPageNumber(), pageBean.getPageSize());
    Page<Software> page = (Page<Software>) softwareMapper.queryResult(software);
    pageBean.setTotal(page.getTotal());
    return BaseDataResult.ok(PageResult.instance(pageBean, page.getResult()));
  }

  @Override
  public BaseResult delete(Integer sid) {
    int result = softwareMapper.delete(sid);
    return result == 1 ? BaseResult.ok("删除成功") : BaseResult.fail("删除失败");
  }

  @Override
  public BaseResult update(Software software) {
    int result = softwareMapper.update(software);
    return result == 1 ? BaseResult.ok("修改成功") : BaseResult.fail("修改失败");
  }

}
